from io import BytesIO
from xml.etree.ElementTree import ElementTree, Element


def read_xml(in_path):
    """

    :param in_path:
    :return:
    """
    tree = ElementTree()
    tree.parse(in_path)
    return tree


def tree_to_string(tree):
    f = BytesIO()
    tree.write(f, encoding="utf-8")
    return f.getvalue()


def write_xml(tree, out_path):
    """

    :param tree:
    :param out_path:
    :return:
    """
    tree.write(out_path, encoding="utf-8", xml_declaration=True)


def if_match(node, kv_map):
    """

    :param node:
    :param kv_map:
    :return:
    """
    for key in kv_map:
        if node.get(key) != kv_map.get(key):
            return False
    return True


# ----------------search -----------------
def find_nodes(tree, path):
    """

    :param tree:
    :param path:
    :return:
    """
    return tree.findall(path)


def get_node_by_keyvalue(nodelist, kv_map):
    """

    :param nodelist:
    :param kv_map:
    :return:
    """
    result_nodes = []
    for node in nodelist:
        if if_match(node, kv_map):
            result_nodes.append(node)
    return result_nodes


def change_node_properties(nodelist, kv_map, is_delete=False):
    """

    :param nodelist:
    :param kv_map:
    :param is_delete:
    :return:
    """
    for node in nodelist:
        for key in kv_map:
            if is_delete:
                if key in node.attrib:
                    del node.attrib[key]
            else:
                node.set(key, kv_map.get(key))


def change_node_text(nodelist, text, is_add=False, is_delete=False):
    """
    :param nodelist: 
    :param text: 
    :param is_add: 
    :param is_delete: 
    :return: 
    """""
    for node in nodelist:
        if is_add:
            node.text += text
        elif is_delete:
            node.text = ""
        else:
            node.text = text


def create_node(tag, property_map, content):
    """

    :param tag:
    :param property_map:
    :param content:
    :return:
    """
    element = Element(tag, property_map)
    element.text = content
    return element


def add_child_node(nodelist, element):
    """

    :param
    nodelist:
    :param
    element:
    :return:
    """
    for node in nodelist:
        node.append(element)


def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
    """

    :param
    nodelist:
    :param
    tag:
    :param
    kv_map:
    :return:
    """
    for parent_node in nodelist:
        children = parent_node.getchildren()
        for child in children:
            if child.tag == tag and if_match(child, kv_map):
                parent_node.remove(child)
